package com.resourcestore.action;
import java.io.UnsupportedEncodingException;

import java.text.ParseException;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.resourcestore.utils.CommonUtil;
import com.resourcestore.utils.tag.Pagination;
import com.resourcestore.action.BaseAction;
import com.resourcestore.dao.CourseFileSrcPublicDao;
import com.resourcestore.model.CourseFileSrcPublic;

public class CourseFileSrcPublicAction extends BaseAction {
	
	private CourseFileSrcPublic courseFileSrcPublic;
	private List<CourseFileSrcPublic> listCourseFileSrcPublic;
	private CourseFileSrcPublicDao courseFileSrcPublicDao = new CourseFileSrcPublicDao();
	private Pagination pagination = new Pagination();

	
	/**
	 * 查询返回List带分页
	 * @throws ParseException 
	 * 
	 */
	public String listCourseFileSrcPublic(){
		courseFileSrcPublic = new CourseFileSrcPublic();
		courseFileSrcPublic.setCourseFileCategoryId(-2);
		pagination.setTotal(courseFileSrcPublicDao.queryCourseFileSrcPublicNum(courseFileSrcPublic));
		listCourseFileSrcPublic = courseFileSrcPublicDao.selectCourseFileSrcPublicPage(courseFileSrcPublic,pagination);
		
		//将bigint数字处理为时间格式
		for (int i = 0; i < listCourseFileSrcPublic.size(); i++) {
			//将文件类型代码转换为名称
			listCourseFileSrcPublic.get(i).setCourseFileTypeName(this.showFileCate(listCourseFileSrcPublic.get(i).getCourseFileType()));	
			try {
					listCourseFileSrcPublic.get(i).setGmtCreatedString(CommonUtil.getSysTimeFormatString(listCourseFileSrcPublic.get(i).getGmtCreated()));
					listCourseFileSrcPublic.get(i).setGmtModifiedString(CommonUtil.getSysTimeFormatString(listCourseFileSrcPublic.get(i).getGmtModified()));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
		}
		
		return "default";
	}
	
	
	/**
	 * 根据条件搜索，查询返回List带分页
	 * @throws ParseException 
	 * 
	 */
	public String searchListCourseFileSrcPublic(){
		
		if (courseFileSrcPublic.getCourseFileCategoryId()==0){
			courseFileSrcPublic.setCourseFileCategoryId(-2);
		}
		
		//对内容进行urldecoder解码
		try {
			courseFileSrcPublic.setCourseFileName(new String(courseFileSrcPublic.getCourseFileName().getBytes("iso-8859-1"),"UTF-8"));
			courseFileSrcPublic.setCourseFileRemark(new String(courseFileSrcPublic.getCourseFileRemark().getBytes("iso-8859-1"),"UTF-8"));
			courseFileSrcPublic.setCourseName(new String(courseFileSrcPublic.getCourseName().getBytes("iso-8859-1"),"UTF-8"));
			courseFileSrcPublic.setAuthor(new String(courseFileSrcPublic.getAuthor().getBytes("iso-8859-1"),"UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		pagination.setTotal(courseFileSrcPublicDao.queryCourseFileSrcPublicNum(courseFileSrcPublic));
		listCourseFileSrcPublic = courseFileSrcPublicDao.selectCourseFileSrcPublicPage(courseFileSrcPublic,pagination);
		
		//将bigint数字处理为时间格式
		for (int i = 0; i < listCourseFileSrcPublic.size(); i++) {
			try {
			listCourseFileSrcPublic.get(i).setGmtCreatedString(CommonUtil.getSysTimeFormatString(listCourseFileSrcPublic.get(i).getGmtCreated()));
			listCourseFileSrcPublic.get(i).setGmtModifiedString(CommonUtil.getSysTimeFormatString(listCourseFileSrcPublic.get(i).getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return newpath("/CourseFileSrcPublic/listCourseFileSrcPublic.jsp");
	}
	
	/**
	 * 预插入一条数据
	  */		
	public String preInsertCourseFileSrcPublic(){
		
		return newpath("/CourseFileSrcPublic/insertCourseFileSrcPublic.jsp");		
	}
	
	/**
	 * 插入一条数据
	  */		
	public String insertCourseFileSrcPublic(){
		
		try {
			courseFileSrcPublic.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			courseFileSrcPublic.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		courseFileSrcPublicDao.insertCourseFileSrcPublic(courseFileSrcPublic);
		return redirect(this.getUrl());		
	}
	
	/**
	 * 更新之前加载原信息
	 * 
	 */
	public String preUpdateCourseFileSrcPublic(){
		courseFileSrcPublic = courseFileSrcPublicDao.selectCourseFileSrcPublicById(courseFileSrcPublic);
		
		return newpath("/CourseFileSrcPublic/updateCourseFileSrcPublic.jsp");
	}
	/**
	 * 更新信息
	 * 
	 */
	public String updateCourseFileSrcPublic(){
		
		try {
			//courseFileSrcPublic.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			courseFileSrcPublic.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		courseFileSrcPublicDao.updateCourseFileSrcPublic(courseFileSrcPublic);
		return redirect(this.getUrl());
	}

	/**
	 * 删除信息
	 */
	public String deleteCourseFileSrcPublic(){
		
		//删除服务器文件
		CourseFileSrcPublic courseFileSrcPublicTemp = new CourseFileSrcPublic();
		courseFileSrcPublicTemp = courseFileSrcPublicDao.selectCourseFileSrcPublicById(courseFileSrcPublic);
		String downloadUrl = courseFileSrcPublicTemp.getDownloadUrl();
		int numFlag = downloadUrl.indexOf("upload/");
		if(numFlag!=-1){
			String fileName = downloadUrl.substring(numFlag).substring(7);
			CommonUtil.deleteFile(ServletActionContext.getServletContext()
					.getRealPath("upload")
					+ "/" + fileName);
		}
		
		courseFileSrcPublicDao.deleteCourseFileSrcPublic(courseFileSrcPublic);
		return redirect(this.getUrl());
	}
	
	/**
	 * 批量删除信息
	 */
	public String deleteMoreCourseFileSrcPublic(){
	
		//删除服务器文件
		List<CourseFileSrcPublic> listTemp = courseFileSrcPublicDao.selectCourseFileSrcPublicByCourseFileIdString(courseFileSrcPublic);
		for(int i=0;i<listTemp.size();i++){
			if(CommonUtil.isNotNullOrNotEqualsEmpty(listTemp.get(i).getDownloadUrl())){
				
				String downloadUrl = listTemp.get(i).getDownloadUrl();
				int numFlag = downloadUrl.indexOf("upload/");
				if(numFlag!=-1){
					String fileName = downloadUrl.substring(numFlag).substring(7);
					CommonUtil.deleteFile(ServletActionContext.getServletContext()
							.getRealPath("upload")
							+ "/" + fileName);
				}
			}
		}
		
		courseFileSrcPublicDao.deleteMoreCourseFileSrcPublic(courseFileSrcPublic);
		return redirect(this.getUrl());
	}

	
	public CourseFileSrcPublic getCourseFileSrcPublic() {
		return courseFileSrcPublic;
	}

	public void setCourseFileSrcPublic(CourseFileSrcPublic courseFileSrcPublic) {
		this.courseFileSrcPublic = courseFileSrcPublic;
	}

	public List<CourseFileSrcPublic> getListCourseFileSrcPublic() {
		return listCourseFileSrcPublic;
	}

	public void setListCourseFileSrcPublic(List<CourseFileSrcPublic> listCourseFileSrcPublic) {
		this.listCourseFileSrcPublic = listCourseFileSrcPublic;
	}
	
	 public Pagination getPagination() {
		return this.pagination;
	 }

	 public void setPagination(Pagination pagination) {
		 this.pagination = pagination;
	 }
}
